# -*- coding: utf-8 -*-
 
import hashlib
from base import UserCase, Runner, TestSuite

class Case(UserCase):
    def device_create(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        return res['data']

    def test_device_package_record_create(self):
        device = self.device_create()
        p = {
            'cpu_id': device['cpu_id'],
            'version': self.randchar(4),
            'url': self.randchar(32),
            'size': self.randnum(4),
        }
        res = self.c.open('/api/device-package-record/create', p)
        self.assertEqual(res['code'], 0)


    def test_device_package_record_list(self):

        device = self.device_create()
        p = {
            'cpu_id': device['cpu_id'],
            'version': self.randchar(4),
            'url': self.randchar(32),
            'size': self.randnum(4),
        }
        res = self.c.open('/api/device-package-record/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device-package-record/list')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'] > 0, True)


    def test_device_package_record_list_filter(self):

        device = self.device_create()
        p = {
            'cpu_id': device['cpu_id'],
            'version': self.randchar(4),
            'url': self.randchar(32),
            'size': self.randnum(4),
        }
        res = self.c.open('/api/device-package-record/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device-package-record/list', {'cpu_id': device['cpu_id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)

        p = {
            'cpu_id': device['cpu_id'],
            'version': self.randchar(4),
            'url': self.randchar(32),
            'size': self.randnum(4),
        }

        res = self.c.open('/api/device-package-record/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device-package-record/list', {'cpu_id': device['cpu_id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)

        res = self.c.open('/api/device-package-record/list', {'cpu_id': device['cpu_id'], 'orderBy': 'device_package_record.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)
        self.assertEqual(res['data']['itemList'][0]['version'], p['version'])
        self.assertEqual(res['data']['itemList'][0]['url'], p['url'])
        self.assertEqual(res['data']['itemList'][0]['size'], int(p['size']))



test_list = [
    'test_device_package_record_create',
    'test_device_package_record_list',
    'test_device_package_record_list_filter',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



